package org.blue.crawler;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.blue.crawler.annotation.CrawlerConfig;
import com.blue.crawler.engine.FileModel;
import com.blue.crawler.engine.ListDetailCrawl;

public class News21cnCrawl implements ListDetailCrawl<News21cnModel>
{
	private static DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
	
	public News21cnCrawl()
	{
	}

	@Override
	public List<News21cnModel> queryList(String html, CrawlerConfig config)
	{
		List<News21cnModel> list = new ArrayList<>();
		Document doc = Jsoup.parse(html);
		Elements divNodes = doc.select("div.mod-pic > div.bd > div.pic-list, div.mod-pic > div.bd > div.art-list");
		for (Element divNode : divNodes)
		{
			News21cnModel model = new News21cnModel();
			Element imgNode = divNode.select("img").first();
			if (imgNode != null)
				model.setCover(imgNode.attr("original"));
			
			Element titleNode = divNode.select("h3 > a").first();
			if (titleNode != null)
			{
				model.setUrl(titleNode.attr("href"));
				model.setTitle(titleNode.text());
			}
			
			Element summaryNode = divNode.select("p.pic-details").first();
			if (summaryNode != null)
				model.setSummary(summaryNode.text());
			
			list.add(model);
		}
		return list;
	}

	@Override
	public List<String> query(String html, CrawlerConfig config, News21cnModel model)
	{
		List<String> picList = new ArrayList<>();
		Document doc = Jsoup.parse(html);
		
		Element timeNode = doc.select("#article-cmt span.pubTime").first();
		if (timeNode != null)
			model.setCreateTime(LocalDateTime.parse(timeNode.text(), dateFormatter));
		
		Element contentNode = doc.getElementById("article_text");
		if (contentNode != null)
		{
			contentNode.select("#embed_hzh_div").remove();
			model.setContent(contentNode.html());
		}
		
		Elements imgNodes = contentNode.select("img");
		if (imgNodes != null)
		{
			for (Element imgNode : imgNodes)
			{
				picList.add(imgNode.attr("src"));
			}
		}
		
		return picList;
	}

	@Override
	public List<String> queryPage(String html, CrawlerConfig config, News21cnModel model, int page)
	{
		return null;
	}

	@Override
	public boolean existUrl(String url)
	{
		return false;
	}

	@Override
	public void handleModel(CrawlerConfig config, News21cnModel doc)
	{
		System.out.printf("title: %s\ncover: %s\ncontent: %s\n", doc.getTitle(), doc.getCover(), doc.getContent());
		if (doc.getFileList() != null && !doc.getFileList().isEmpty())
		{
			System.out.println("picList: ");
			for (FileModel model : doc.getFileList())
			{
				System.out.println(model.getPath());
			}
		}
		System.out.println("=======================");
	}
}
